﻿using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Windows.Controls;

namespace Alfa.Common.ValidationRules {
	public class StringRegularExpressionValidationRule : ValidationRuleBase {
		public StringRegularExpressionValidationRule( ) { }

		public string Regex { get; set; }

		public override ValidationResult Validate(object value, CultureInfo cultureInfo) {
			if (string.IsNullOrEmpty(this.Regex))
				throw new Exception("Regular expression is empty.");
			if (value == null)
				return this.ParseError(true, null);

			Regex regex = new Regex(this.Regex);

			if (!regex.IsMatch((string)value)) {
				if (ErrorMessage != null && ErrorMessage != "")
					return this.ParseError(false, ErrorMessage);
				else
					return this.ParseError(false, Messages.PleaseEnterCorrectValue);
			} else
				return this.ParseError(true, null);
		}
	}
}
